package com.bhs.sansong.ui.order;

import static android.content.Context.CLIPBOARD_SERVICE;

import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.view.View;

import androidx.annotation.Nullable;

import com.bhs.sansong.R;
import com.bhs.sansong.adapter.OrderGoodsAdapter;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.base.BaseBean;
import com.bhs.sansong.bean.OrderGoodsBean;
import com.bhs.sansong.net.NetCallBack;
import com.bhs.sansong.ui.dialog.LogisticsTrackDialog;
import com.bhs.sansong.ui.dialog.MyClickListener;
import com.bhs.sansong.ui.orderDispose.OrderEvaluateActivity;
import com.bhs.sansong.ui.shipments.IssueNewOrderActivity;
import com.bhs.sansong.ui.wallet.PayDeskActivity;
import com.bhs.sansong.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnConfirmListener;

import okhttp3.ResponseBody;
import retrofit2.Response;

public class OrderOperation implements NetCallBack {
    private ClipData mClipData;   //剪切板Data对象
    private ClipboardManager mClipboardManager;   //剪切板管理工具类
    private BaseActivity activity;
    private OrderGoodsAdapter mAdapter;
    private int mPosition;

    public OrderOperation(BaseActivity activity) {
        this.activity = activity;
        // 1. 注册mClipboardManager
        mClipboardManager = (ClipboardManager) activity.getSystemService(CLIPBOARD_SERVICE);
    }

    public void operation(OrderGoodsAdapter mAdapter) {
        this.mAdapter = mAdapter;
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                activity.startActivity(new Intent(activity, OrderDetailsActivity.class)
                        .putExtra("deliver_sn", mAdapter.getData().get(position).getDeliver_sn()));
            }
        });

        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @SuppressLint("NonConstantResourceId")
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                OrderGoodsBean data = mAdapter.getData().get(position);
                switch (view.getId()) {
                    case R.id.tv_order_number:
                        //创建一个新的文本clip对象
                        mClipData = ClipData.newPlainText("Simple text", data.getDeliver_sn());
                        //把clip对象放在剪贴板中
                        mClipboardManager.setPrimaryClip(mClipData);
                        ToastUtil.show("复制成功");
                        break;
                    case R.id.btn_one_more:
                        activity.startActivity(new Intent(activity, IssueNewOrderActivity.class)
                                .putExtra("deliver_sn", data.getDeliver_sn()));
                        break;
                    case R.id.btn_relaunch:
                        new XPopup.Builder(activity)
                                .asConfirm("提示", "确认再次发起吗？",
                                        "取消", "确定",
                                        new OnConfirmListener() {
                                            @Override
                                            public void onConfirm() {
                                                //再次发起
                                                mPosition = position;
                                                data.setStatus(3);
                                                data.setStatus_msg("待接单");
                                                activity.networkRequest(activity.netApi.reapply_order(data.getDeliver_sn()),
                                                        OrderOperation.this, 101);
                                            }
                                        }, null, false)
                                .show();

                        break;
                    case R.id.btn_confirm_receipt:
                        new XPopup.Builder(activity)
                                .asConfirm("提示", "确认收货吗？",
                                        "取消", "确定",
                                        new OnConfirmListener() {
                                            @Override
                                            public void onConfirm() {
                                                //确认收货
                                                mPosition = position;
                                                data.setStatus(20);
                                                data.setStatus_msg("已完成");
                                                activity.networkRequest(activity.netApi.confirm_receipt(data.getDeliver_sn()),
                                                        OrderOperation.this, 101);
                                            }
                                        }, null, false)
                                .show();

                        break;
                    case R.id.btn_check_logistics:
                        logisticsTrack(data.getDeliver_sn());
                        break;
                    case R.id.btn_pay:
                        activity.startActivity(new Intent(activity, PayDeskActivity.class)
                                .putExtra("pay_sn", data.getPay_sn())
                                .putExtra("deliver_sn", data.getDeliver_sn())
                                .putExtra("money", data.getNo_pay_total()));
                        break;
                    case R.id.btn_evaluate:
                        activity.startActivity(new Intent(activity, OrderEvaluateActivity.class)
                                .putExtra("deliver_sn", data.getDeliver_sn()));
                        break;

                }
            }
        });

    }


    private void logisticsTrack(String deliver_sn) {

        LogisticsTrackDialog trackDialog = new LogisticsTrackDialog(activity, deliver_sn);
        trackDialog.show();
            trackDialog.setMyClickListener(new MyClickListener() {
                @Override
                public void onClick(View v) {
                    //创建一个新的文本clip对象
                    mClipData = ClipData.newPlainText("Simple text", deliver_sn);
                    //把clip对象放在剪贴板中
                    mClipboardManager.setPrimaryClip(mClipData);
                    ToastUtil.show("复制成功");
                }
            });


    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        if (requestCode == 101) {
            BaseBean bean = new Gson().fromJson(json, BaseBean.class);
            if (bean.isStatus()) {
                if (listener != null) {
                    listener.onClick(null);
                }
                mAdapter.notifyItemChanged(mPosition);
            } else {
                ToastUtil.show(bean.getMsg());
            }

        }
    }

    @Override
    public void onFailure(int requestCode) {

    }


    private MyClickListener listener;

    public void setMyClickListener(@Nullable MyClickListener l) {
        this.listener = l;
    }
}
